package customer.zspt.cn.customer.utils;

import android.content.Context;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/6/12 0012.
 */

public class GetAllInfoUtils {
    public static GetAllInfoUtils infoUtils = new GetAllInfoUtils();
    private OkHttpClient client;
    /**需要复用的信息*/
    public  void getInfo(String url, String token,final Context context,final String key) {
        client = new OkHttpClient();
        client.newBuilder().readTimeout(3000, TimeUnit.SECONDS).connectTimeout(3000,TimeUnit.SECONDS).build();
        Request request = new Request.Builder().addHeader("Authorization", token).
                addHeader("Accept","application/json").addHeader("Content-Type","application/json; charset=UTF-8;").addHeader("Connection","keep-alive").
                url(url).build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Call call, IOException e) {
                if(mLinster!=null)
                {
                    mLinster.fail();
                }
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                try {
                    JSONObject resultObj = new JSONObject(result);
                    if(resultObj.getInt("code")==0)
                    {
                        if(mLinster!=null)
                        {
                            mLinster.success(result);
                        }
                        SPUtils.setString(context, key,result);
                    }
                    else{
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    if(mLinster!=null)
                    {
                        mLinster.fail();
                    }
                }
            }
        });
    }
    /**获取基本信息*/
    public  void getFriendInfo(String url, String token,final Context context) {
        client = new OkHttpClient();
        client.newBuilder().connectTimeout(3000,TimeUnit.SECONDS).
                readTimeout(3000,TimeUnit.SECONDS).build();
        Request request = new Request.Builder().addHeader("Authorization", token).
                addHeader("Accept","application/json").addHeader("Content-Type","application/json; charset=UTF-8;").addHeader("Connection","keep-alive").
                url(url).build();
        client.newCall(request).enqueue(new Callback(){
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("e",call+"");
                if(mLinster!=null)
                {
                    mLinster.fail();
                }
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.e("result",result);
                try {
                    JSONObject resultObj = new JSONObject(result);
                    if(resultObj.getInt("code")==0)
                    {
                        if(mLinster!=null)
                        {
                            mLinster.success(result);
                        }
                    }
                    else{

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e("e",e+"");
                    if(mLinster!=null)
                    {
                        mLinster.fail();
                    }
                }

            }
        });
    }
    public ReadyData mLinster;
    public void monitoringLinster(ReadyData mLinster)
    {
        this.mLinster = mLinster;
    }

//数据是否准备好的接口
public interface ReadyData
{
    void success(String data);
    void fail();
}
}